package com.carkeeper.subject.task;

import com.carkeeper.common.utils.QiniuUtils;
import com.carkeeper.common.utils.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Set;

@Component
public class TimerClearImg {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*
     * 实现定时清理垃圾图片
     * */
    @Scheduled(cron = "0 0 12 * * ?")
    public void clearImg(){
        //根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> set = stringRedisTemplate.boundSetOps(RedisConstant.PIC_RESOURCES).diff(RedisConstant.PIC_DB_RESOURCES);
        System.out.println(set);
        if (set != null) {
            for (String picName : set) {
                //删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(picName);
                //从Redis集合中删除图片名称
                stringRedisTemplate.boundSetOps(RedisConstant.PIC_RESOURCES).remove(picName);
            }
        }
    }
}
